﻿using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Text;

namespace HttpLib.Services
{
    public class IdentityService : IIdentityService
    {
        private IHttpContextAccessor _context;
        private ILogger<IdentityService> _logger;
        public IdentityService(
            IHttpContextAccessor context,
            ILogger<IdentityService> logger)
        {
            _context = context ?? throw new ArgumentNullException(nameof(context));
            _logger = logger ?? throw new ArgumentNullException(nameof(logger));
        }

        public string GetUserIdentity()
        {
            try
            {
                return _context.HttpContext.User.FindFirst("sub").Value;
            }
            catch(Exception e)
            {
                _logger.LogError($"GetUserIdentity Error:{e.Message}", null);
                return string.Empty;
            }
           
        }

        public string GetUserName()
        {
            try
            {
                return _context.HttpContext.User.Identity.Name;
            }
            catch(Exception e)
            {
                _logger.LogError($"GetUserName Error:{e.Message}", null);
                return string.Empty;
            }
        }
    }
}
